unless defined?(RediPress) && RediPress.cli?
  raise "You can't load this file. Please load the redipress/cli file instead."
end

module RediPress
  module CLI
    module Helper
      # Prepare before running a task
      #
      # Example:
      #   >> prepare
      #   => nil
      #
      def prepare
        # Set the SSH Kit output verbosity to debug if verbose is enabled
        SSHKit.config.output_verbosity = Logger::DEBUG if options[:verbose]

        nil
      end

      # Show a spinner while the block equates to true
      #
      # Example:
      #   >> spin_while("Doing something") { true }
      #   => nil
      #
      def spin_while(message, &block)
        return nil unless block_given?

        spinner = TTY::Spinner.new("#{message} ", format: :spin_1)

        while yield do
          spinner.spin
          sleep(0.1)
        end

        spinner.stop("\u2713\n\n")

        nil
      end

      # Print a new line
      #
      # Example:
      #   >> new_line
      #   => "\n"
      #
      def new_line
        puts ""
      end

      # Get a prompt
      #
      # Example:
      #   >> prompt
      #   => #<TTY::Prompt:0x00000000000000>
      #
      def prompt
        @prompt ||= TTY::Prompt.new
      end
    end
  end
end
